<template>
  <div>
    <elm-table :data="tableData" :page-size="9999" :show-index="false" style="height: 57vh;margin: 10px" v-loading="loading" v-if="tableData">
      <el-table-column prop="name" label="进货批次" align="center" header-align="center"></el-table-column>
      <el-table-column prop="unit" label="单位" align="center" header-align="center" width="50"></el-table-column>
      <el-table-column prop="quantity" label="销售数量" align="center" :formatter="formatCurrency"></el-table-column>
      <el-table-column prop="price" label="进货价格" align="center" :formatter="formatCurrency"></el-table-column>
      <el-table-column prop="sale_price" label="销售价格" align="center" :formatter="formatCurrency"></el-table-column>
      <el-table-column prop="tex" label="税率" align="center" header-align="center" width="50">
        <template v-slot="scope">
          {{scope.row.tex?scope.row.tex+"%":""}}
        </template>
      </el-table-column>
      <el-table-column prop="in_price" label="进货额" align="center" :formatter="formatCurrency"></el-table-column>
      <el-table-column prop="in_tex" label="进项税" align="center" :formatter="formatCurrency"></el-table-column>
      <el-table-column prop="in_total" label="含税进货额" align="center" width="100" :formatter="formatCurrency"></el-table-column>
      <el-table-column prop="out_price" label="销售额" align="center" :formatter="formatCurrency"></el-table-column>
      <el-table-column prop="out_tex" label="销项税" align="center" :formatter="formatCurrency"></el-table-column>
      <el-table-column prop="out_total" label="含税销售额" align="center" width="100" :formatter="formatCurrency"></el-table-column>
      <el-table-column prop="profit" label="毛利润" align="center" :formatter="formatCurrency"></el-table-column>
      <el-table-column prop="difference_tex" label="差进项税" align="center" :formatter="formatCurrency"></el-table-column>
    </elm-table>
  </div>
</template>

<script>
export default {
  extends: elmVueBase,
  data() {
    return {
      tableData:{},
      outData:elm.dialog.request.outData,
      loading:false
    }
  },
  async created() {
    this.getTableData();
  },
  methods: {
    async getTableData(){
      this.loading=true;

      this.tableData=[];
      let total={
        name:"总计",
        unit:this.outData.unit,
        quantity:0,
        sale_price:this.outData.price,
        tex:this.outData.tex,
        in_price:0,
        in_tex:0,
        in_total:0,
        out_price:0,
        out_tex:0,
        out_total:0,
        profit:0,
        difference_tex:0
      }

      let res=await elm.post("/business/jxc/goods/out/stage/GetData", {out_id:this.outData.id});
      for(let i in res){
        let item=res[i];
        item.in_price=(item.quantity||0)*(item.price||0);
        item.in_tex=(item.quantity||0)*(item.price||0)*(item.tex||0)/100;
        item.in_total=item.in_price+item.in_tex;
        item.out_price=(this.outData.price||0)*(item.quantity||0);
        item.out_tex=item.out_price*(this.outData.tex||0)/100;
        item.out_total=item.out_price+item.out_tex;
        item.profit=item.out_price-item.in_total;
        item.difference_tex=item.out_tex-item.in_tex;
        item.sale_price=this.outData.price;
        this.tableData.push({...item});

        total.quantity += item.quantity || 0;
        total.in_price += item.in_price || 0;
        total.in_tex += item.in_tex || 0;
        total.in_total += item.in_total || 0;
        total.out_price += item.out_price || 0;
        total.out_tex += item.out_tex || 0;
        total.out_total += item.out_total || 0;
        total.profit += item.profit || 0;
        total.difference_tex += item.difference_tex || 0;
      }
      total.price=(total.in_price||0)/(total.quantity||1);
      this.tableData.push(total);

      this.loading=false;
    },
    formatCurrency(row, column, cellValue, index) {
      if (cellValue == null) return '0';
      return Number(parseFloat(cellValue).toFixed(2));
    }
  }
}
</script>
